var pageCount = 1;
function classOnload(a){
    console.log(a);
// }


var Json={
    childProjectId:Number(localStorage.getItem("childProjectId")),
    pageIndex:1,
    pageSize:7
} 
ajax.post(ajax.myUrl+"project/findTask",Json,function(result){
    console.log(result);
    response(result);
    var data = result.dataset.taskList;
    // console.log(data)
    var tbody = document.querySelector("tbody");
    tbody.innerHTML=``
    if(data.length==0){
        tbody.innerHTML=``
    }
    for(var i=0;i<data.length;i++){
        var newtr = document.createElement("tr");
        newtr.innerHTML=`
        <td>${data[i].taskId}</td>
            <td>${data[i].taskName}</td>
            <td>${data[i].taskLeader}</td>
            <td>${data[i].workingHours}</td>
            <td>${data[i].startDate}</td>
            <td>${data[i].endDate}</td>
            <td>${data[i].progress+"%"}</td>
            <td>
                <div class="more">修改</div>
                <div class="more2">删除</div>
            </td>
         
         `;
         tbody.appendChild(newtr);
        }
           //点击修改
           localStorage.setItem("taskId","")
           var more = document.querySelectorAll(".more");
           var addProject2 = document.querySelector("#addProject2");
           var mask = document.querySelector("#mask");
           for(var i=0;i<more.length;i++){
               more[i].onclick=function(){
               
                   for(var j=0;j<more.length;j++){
                       if(this==more[j]){
                           addProject2.setAttribute("class","projectShow");
                           mask.setAttribute("class","maskShow");
                           localStorage.setItem("taskId",data[j].taskId)
                       }
                   }
               }
           }
        //    点击删除
        var del = document.getElementsByClassName("more2");
        console.log(del);
        for(var x=0;x<del.length;x++){
            del[x].onclick = function(){
                for(var y=0;y<del.length;y++){
                    if(this == del[y]){
                        localStorage.setItem("taskId",data[y].taskId);
                        var dataone = Number(localStorage.getItem("taskId"))
                        var xhr2 = new XMLHttpRequest();
                        xhr2.open("GET","http://192.168.192.100:8080/BaorOASys/project/deleteTask?taskId="+dataone);
                        xhr2.send();
                        xhr2.onreadystatechange = function(){
                            if(xhr2.readyState==4 && xhr2.status==200){
                                console.log(result)
                            }
                        }
                    }
                }
            }
        }
        var addProject2 = document.querySelector("#addProject2");
        var btn1 = addProject2.querySelectorAll("button")[0];
        var btn2 = addProject2.querySelectorAll("button")[1];
        // 点击确定提交修改内容
        btn1.onclick=function(){
            var addProject2 = document.querySelector("#addProject2");
            addProject2.setAttribute("class","projectShowNone");
            mask.setAttribute("class","maskShowNone");
            var inputs2=document.querySelectorAll("input")
            var Json3={
                taskName:inputs2[8].value,
                taskLeader:Number(selects.value),
                workingHours:Number(inputs2[9].value),
                startDate:inputs2[10].value,
                endDate:inputs2[11].value,
                prop:parseFloat(inputs2[12].value),
                childProjectId:Number(localStorage.getItem("childProjectId")),
                userId:18,// taskleader的值 和tasskid一一对应
                taskId:Number(localStorage.getItem("taskId")),//当前任务id
                // progress:10//当前任务进程
            }
            console.log(Json3,"修改")
            ajax.post("http://192.168.192.100:8080/BaorOASys/project/updateTask",Json3,function(result){
                console.log(result)
            })
        }
        btn2.onclick=function(){
            // var addProject2 = document.querySelector("#addProject2");
            addProject2.setAttribute("class","projectShowNone");
            mask.setAttribute("class","maskShowNone");
            
        }
     
        
    var selects = document.querySelector("select");
    var selects2 =document.querySelectorAll("select")[1];
    // console.log(localStorage.getItem("freeUser"));
    var arr1=localStorage.getItem("freeUser").split(",");
    // console.log(arr1)
//    console.log(localStorage.getItem("freeId"));
   var arr2=localStorage.getItem("freeId").split(",");
//    console.log(arr2)
   for(var j=0;j<arr1.length;j++){
    selects.add(new Option(arr1[j],arr2[j]))
    selects2.add(new Option(arr1[j],arr2[j]))
   }
   var addition = document.querySelector(".addition");
var task = addition.querySelector("div");
var addProject3 = document.getElementById("addProject3");
var btn3 =addProject3.querySelectorAll("button")[0];
var btn4 =addProject3.querySelectorAll("button")[1];
task.onclick = function(){
    task.style.color="#0F6AB1";
    addProject3.setAttribute("class","projectShow");
    mask.setAttribute("class","maskShow");
}
btn3.onclick=function(){
    // var addProject2 = document.querySelector("#addProject2");
    task.style.color="#000";
    addProject3.setAttribute("class","projectShowNone");
    mask.setAttribute("class","maskShowNone"); 
    var inputs = document.querySelectorAll("input");
    var Json2={
        taskName:inputs[13].value,
        taskLeader:Number(selects.value),
        workingHours:Number(inputs[14].value),
        startDate:inputs[15].value,
        endDate:inputs[16].value,
        childProjectId:Number(localStorage.getItem("childProjectId")),
        prop:parseFloat((inputs[17].value))
    }
    console.log(Json2,"添加")
    ajax.post("http://192.168.192.100:8080/BaorOASys/project/addProjectTask",Json2,function(result){

    })
}
btn4.onclick=function(){
    // var addProject2 = document.querySelector("#addProject2");
    task.style.color="#000";
    addProject3.setAttribute("class","projectShowNone");
    mask.setAttribute("class","maskShowNone");
}
})

}
// 搜索
var search = document.querySelector("button")
search.onclick=function(){
    // console.log(document.querySelectorAll("input"))
    var startime = null;
    var endtime = null;
    if(document.querySelectorAll("input")[1].value==""){
        startime=null
    }else{
        startime=document.querySelectorAll("input")[1].value
    }
    if(document.querySelectorAll("input")[2].value==""){
        endtime=null
    }else{
        endtime=document.querySelectorAll("input")[2].value
    }
    var Json4={
        childProjectId:Number(localStorage.getItem("childProjectId")),
        pageIndex:1,
        pageSize:5,
        taskName:document.querySelectorAll("input")[0].value,
        time1:startime,
        time2:endtime
        
    }
    // console.log(Json4)
    ajax.post("http://192.168.192.100:8080/BaorOASys/project/findTask",Json4,function(result){
        // console.log(result)
        var data = result.dataset.taskList;
    // console.log(data)
    var tbody = document.querySelector("tbody");
    tbody.innerHTML=``
    if(data.length==0){
        tbody.innerHTML=``
    }
    for(var i=0;i<data.length;i++){
        var newtr = document.createElement("tr");
        newtr.innerHTML=`
        <td>${data[i].taskId}</td>
            <td>${data[i].taskName}</td>
            <td>${data[i].taskLeader}</td>
            <td>${data[i].workingHours}</td>
            <td>${data[i].startDate}</td>
            <td>${data[i].endDate}</td>
            <td>${data[i].progress+"%"}</td>
            <td>
                <div class="more">修改</div>
                <div class="more2">删除</div>
            </td>
         
         `;
         tbody.appendChild(newtr);
        }
    })
    
}

